/*
 * MIT License
 *
 * Copyright (c) 2023 芊芊墨客
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package com.zhangmeng.tools.controller;

import com.zhangmeng.tools.utils.AlertUtils;
import com.zhangmeng.tools.utils.ResourcesUtils;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javafx.util.StringConverter;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * @author : 芊芊墨客
 * @version : 1.0
 * @date : 2023-02-16 09:19
 */
@Slf4j
public class VipParserController {

    @FXML
    private ComboBox<URLS> comboBox;

    @FXML
    private TextField input_text;

    @FXML
    public void initialize() {

        ObservableList<URLS> list = FXCollections.observableArrayList();
        URLS[] values = URLS.values();
        list.addAll(values);

        comboBox.setConverter(new StringConverter<URLS>() {
            @Override
            public String toString(URLS object) {
                if (object == null) {
                    return "";
                }
                return object.getName();
            }

            @Override
            public URLS fromString(String string) {
                return null;
            }
        });

        comboBox.setItems(list);
        comboBox.getSelectionModel().select(4);


    }

    @FXML
    public void player() {
        log.info("点击解析:{}", comboBox.getSelectionModel().getSelectedItem().name);

        if (input_text.getText().length() == 0){
            AlertUtils.alert_warning("请输入解析地址!");
            return;
        }

        String url = comboBox.getSelectionModel().getSelectedItem().url;
        String path = url + input_text.getText();

        AnchorPane root = null;
        try {
            root = FXMLLoader.load(ResourcesUtils.getResource("vip-parser-player"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        WebView webView = (WebView) root.lookup("#webView");
        WebEngine engine = webView.getEngine();
        engine.load(path);

        Stage stage = (Stage) this.comboBox.getScene().getWindow();
        stage.hide();

        Scene scene = new Scene(root);
        Stage player_stage = new Stage();
        player_stage.setScene(scene);
        player_stage.setTitle("播放");
        player_stage.setWidth(1600);
        player_stage.setHeight(800);
        player_stage.setFullScreen(true);
        player_stage.setOnCloseRequest(windowEvent -> {
            log.info("播放关闭");
            stage.show();
        });
        player_stage.show();
    }

    public enum URLS {

        vip1717yun("1717yun解析", "https://www.1717yun.com/jx/ty.php?url=", "稳定通用"),
        wpsseo("wpsseo", "http://www.wpsseo.cn/jx/?url=", "稳定通用"),
        cjw123("cjw123", "https://a.cjw123.com/index.php?url=", "稳定通用"),
        xmflv("xmflv", "https://jx.xmflv.com/?url=", "稳定通用"),
        jsonplayer("jsonplayer", "https://jx.jsonplayer.com/player/?url=", "稳定通用"),
        vip1717yun_2("VIP视频解析116kan智能解析", "https://www.1717yun.com/api/?url=", "vip解析");


        private String name;
        private String url;
        private String desc;

        URLS(String name, String url, String desc) {
            this.name = name;
            this.url = url;
            this.desc = desc;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }
    }
}
